package lex.expression;

import java.util.HashMap;
import java.util.Map;

public class Token
{
	public static final Token END = new Token( TokenType.END, "" );
	public static final Token UNKOWN = new Token( TokenType.UNKOWN, "" );
	private static final Map<Character, Token> cache = new HashMap<Character, Token>();
	
	
	private TokenType tokenType = TokenType.UNKOWN;
	private String image = "";
	
	public static Token parseOperatorToken( char operator )
	{
		Token token = cache.get( operator );
		if( null == token )
		{
			token = new Token( operator );
			cache.put( operator, token );
		}
		return token;
	}
	
	private Token( char operator )
	{
		this( TokenType.parseOperatorType( operator ), String.valueOf( operator ) );
	}
	
	public Token( TokenType type, String image )
	{
		this.tokenType = type;
		this.image = image;
	}

	public String getImage()
	{
		return image;
	}

	public TokenType getTokenType()
	{
		return tokenType;
	}
	
	public boolean isOperator()
	{
		return this.tokenType.isOperator();
	}
	
	public boolean isOperand()
	{
		return this.tokenType.isOperand();
	}
	
	public String toString()
	{
		return this.image + "\t[" + this.tokenType.name() + "]";
	}
}
